<?php

namespace Home\Controller;

class CodeController extends CommonController {
	
	// 更新数据
	public function js_update() {
		
		//$data1['code_key']=getNewCokeKey();
// 		var_dump(getNewCokeKey());
// 		die();
		dump("执行了js_update");
		$opts = array(
				'http'=>array(
						'method'=>"GET",
						'timeout'=>1,//单位秒
				)
		);
		$cnt=0;
		while($cnt<3 && ($code_file=file_get_contents("http://chart.cp.360.cn/zst/qkj/?lotId=255401&issue=", false, stream_context_create($opts)))===FALSE) $cnt++;
		
		$session_code_file = $code_file;
		$code_object = json_decode($session_code_file);
		$code_array = object_array($code_object);
// 		dump($code_file);
// 		dump($code_array[0]['Issue']);
// 		dump(S('code_key',null));
// 		die();
		if($code_array[0]['Issue']==''||$code_array[0]['WinNumber']==null){
			die();
		}
		
		if(S('code_key')==$code_array[0]['Issue']){
			die();
		}else {
			//存数据库
			$codeModel = D("Code");
			$code_talModel = D("CodeTal");
			$issue = $code_array[0]['Issue'];
			$data['code_key']='20'.$issue;
			$data['date']='20'.substr($issue,0,6);
			$data['number']=substr($issue, 6,3);
			$data['hm']=$code_array[0]['WinNumber'];
// 			dump($data);
// 			$hm_arr = explode(',',$code_array[0]['WinNumber']);
			$data['h1'] = substr($data['hm'], 0,1);
			$data['h2'] = substr($data['hm'], 1,1);
			$data['h3'] = substr($data['hm'], 2,1);
			$data['h4'] = substr($data['hm'], 3,1);
			$data['h5'] = substr($data['hm'], 4,1);
// 			dump($data);
// 			die();
			//后2
			if($data['h5']==$data['h4']){
				$data['hou2']='对子';
			}else{
				$data['hou2']='';
			}
// 		后3(组六，组三，豹子)
			if($data['h3']==$data['h4']&&$data['h4']==$data['h5']){
				$data['hou3']='豹子';
			}else if($data['h3']==$data['h4']||$data['h3']==$data['h5']||$data['h4']==$data['h5']){
				$data['hou3']='组三';
			}else{
				$data['hou3']='组六';
			}
			$data['time']=date("Y-m-d H:i:s",time());
// 			dump($data);
// 			die();
			$result_add = $codeModel->add($data);
			$code_talModel->add($data);
			if($result_add===false){
				$result_save = $codeModel->save($data);
				$code_talModel->save($data);
			}
			S('code_key',$issue);//最新一期开奖
			$this->tatollr();//统计冷热
			$this->tatolyl();//统计遗漏
			$this->sjtj();//随机推荐
			$this->sjtjall();//随机推荐所有1－10
		}
	}
	
	
	//统计出现次数
	function tatollr(){
		dump("执行了tatollr");
		
		$total_arr=array(10,20,50,100,500,1000);
		foreach ($total_arr as $k_total => $v_total) {
			$codeModel = D("CodeTal");
				$number = $codeModel->limit(($v_total),1)->order('code_key desc')->select()[0]['code_key'];
				//统计个位
				for($i=0;$i<=9;$i++){
					$where_ge['h5']=$i;
					$where_ge['code_key']=array('gt',$number);
					$result= $codeModel->where($where_ge)->order('code_key desc')->count();
					$data_ge['n'.$i]=$result;
				}
				$data_ge['code_key']=$codeModel->limit(0,1)->order('code_key desc')->select()[0]['code_key'];
				$data_ge['total']=$v_total;
				$data_ge['time']=date("Y-m-d H:i:s",time());
				$lrModel = D("LrGe");
				$result_save = $lrModel->save($data_ge);
				if($result_save==0){
					$result_add = $lrModel->add($data_ge);
				}
				//统计十位
				for($i=0;$i<=9;$i++){
					$where_shi['h4']=$i;
					$where_shi['code_key']=array('gt',$number);
					$result= $codeModel->where($where_shi)->order('code_key desc')->count();
					$data_shi['n'.$i]=$result;
				}
				$data_shi['code_key']=$codeModel->limit(0,1)->order('code_key desc')->select()[0]['code_key'];
				$data_shi['total']=$v_total;
				$data_shi['time']=date("Y-m-d H:i:s",time());
				$lrModel = D("LrShi");
				$result_save = $lrModel->save($data_shi);
				if($result_save==0){
					$result_add = $lrModel->add($data_shi);
				}
				//统计bai位
				for($i=0;$i<=9;$i++){
					$where_bai['h3']=$i;
					$where_bai['code_key']=array('gt',$number);
					$result= $codeModel->where($where_bai)->order('code_key desc')->count();
					$data_bai['n'.$i]=$result;
				}
				$data_bai['code_key']=$codeModel->limit(0,1)->order('code_key desc')->select()[0]['code_key'];
				$data_bai['total']=$v_total;
				$data_bai['time']=date("Y-m-d H:i:s",time());
				$lrModel = D("LrBai");
				$result_save = $lrModel->save($data_bai);
				if($result_save==0){
					$result_add = $lrModel->add($data_bai);
				}
				//统计qian位
				for($i=0;$i<=9;$i++){
					$where_qian['h2']=$i;
					$where_qian['code_key']=array('gt',$number);
					$result= $codeModel->where($where_qian)->order('code_key desc')->count();
					$data_qian['n'.$i]=$result;
				}
				$data_qian['code_key']=$codeModel->limit(0,1)->order('code_key desc')->select()[0]['code_key'];
				$data_qian['total']=$v_total;
				$data_qian['time']=date("Y-m-d H:i:s",time());
				$lrModel = D("LrQian");
				$result_save = $lrModel->save($data_qian);
				if($result_save==0){
					$result_add = $lrModel->add($data_qian);
				}
				//统计wan位
				for($i=0;$i<=9;$i++){
					$where_wan['h1']=$i;
					$where_wan['code_key']=array('gt',$number);
					$result= $codeModel->where($where_wan)->order('code_key desc')->count();
					$data_wan['n'.$i]=$result;
				}
				$data_wan['code_key']=$codeModel->limit(0,1)->order('code_key desc')->select()[0]['code_key'];
				$data_wan['total']=$v_total;
				$data_wan['time']=date("Y-m-d H:i:s",time());
				$lrModel = D("LrWan");
				$result_save = $lrModel->save($data_wan);
				if($result_save==0){
					$result_add = $lrModel->add($data_wan);
				}
		}
		
		$this->gs_tj();//公式推荐
	}
	
	function gs_tj(){
		dump("执行了gs_tj");
		$totalarr = array(10,20,50,100,500,1000);
		foreach ($totalarr as $k_totalarr => $v_totalarr) {
			$marr = array(array('LrGe','ge'),array('LrShi','shi'),array('LrBai','bai'),array('LrQian','qian'),array('LrWan','wan'));
			//推荐个位
			foreach ($marr as $k_marr => $v_marr) {
				$lrMode = D($v_marr[0]);
				$where['total']=$v_totalarr;
				$result_find= $lrMode->where($where)->find();
				$lrarr = array();
				for($i=0;$i<=9;$i++){
					$lrarr['n'.$i]=$result_find['n'.$i];
				}
				arsort($lrarr);
				$re_key='';
				foreach ($lrarr as $k_lrarr => $v_lrarr) {
					$re_key=$re_key.substr($k_lrarr, 1,1);
				}
				$data1['code_key']=getNextId($result_find['code_key']);
// 				$data1['code_key']=getNewCokeKey();
				var_dump(getNewCokeKey());
				$data1[$v_marr[1]]=$re_key;
				asort($lrarr);
				$leng_key='';
				foreach ($lrarr as $k_lrarr => $v_lrarr) {
					$leng_key=$leng_key.substr($k_lrarr, 1,1);
				}
				$data2['code_key']=getNextId($result_find['code_key']);
// 				$data2['code_key']=getNewCokeKey();
				$data2[$v_marr[1]]=$leng_key;
			}
			$tjModel = D("GsTjr".$v_totalarr);
			$result_save1 = $tjModel->save($data1);
			if($result_save1==0){
				$result_add = $tjModel->add($data1);
			}
			$tjModel = D("GsTjl".$v_totalarr);
			$result_save2 = $tjModel->save($data2);
			if($result_save2==0){
				$result_add = $tjModel->add($data2);
			}
		}
	}
	
	//统计遗漏
	function tatolyl(){
		dump("执行了tatoyl");
		//计算个位的0~９上次出现是到现在间了多少期
		$codeModel=D("CodeTal");
		$code_key = $codeModel->order('code_key desc')->find()['code_key'];
		//统计个位
		for($i=0;$i<=9;$i++){
			$where_ge['h5']=$i;
			$code_key_n= $codeModel->where($where_ge)->order('code_key desc')->find()['code_key'];
			$where_n1_c['code_key']=array('gt',$code_key_n);
			$n = $codeModel->where($where_n1_c)->count();
			$data_ge['n'.$i]=$n;
		}
		$ylModel = D("YlGe");
		$data_ge['code_key']=$code_key;
		$data_ge['time']=date("Y-m-d H:i:s",time());
		$result_save_ge = $ylModel->save($data_ge);
		if($result_save_ge==0){
			$result_add_ge = $ylModel->add($data_ge);
		}
		//统计shi位
		for($i=0;$i<=9;$i++){
			$where_shi['h4']=$i;
			$code_key_n= $codeModel->where($where_shi)->order('code_key desc')->find()['code_key'];
			$where_n1_c['code_key']=array('gt',$code_key_n);
			$n = $codeModel->where($where_n1_c)->count();
			$data_shi['n'.$i]=$n;
		}
		$ylModel = D("YlShi");
		$data_shi['code_key']=$code_key;
		$data_shi['time']=date("Y-m-d H:i:s",time());
		$result_save_shi = $ylModel->save($data_shi);
		if($result_save_shi==0){
			$result_add_shi = $ylModel->add($data_shi);
		}
		//统计shi位
		for($i=0;$i<=9;$i++){
			$where_bai['h3']=$i;
			$code_key_n= $codeModel->where($where_bai)->order('code_key desc')->find()['code_key'];
			$where_n1_c['code_key']=array('gt',$code_key_n);
			$n = $codeModel->where($where_n1_c)->count();
			$data_bai['n'.$i]=$n;
		}
		$ylModel = D("YlBai");
		$data_bai['code_key']=$code_key;
		$data_bai['time']=date("Y-m-d H:i:s",time());
		$result_save_bai = $ylModel->save($data_bai);
		if($result_save_bai==0){
			$result_add_bai = $ylModel->add($data_bai);
		}
		//统计shi位
		for($i=0;$i<=9;$i++){
			$where_qian['h2']=$i;
			$code_key_n= $codeModel->where($where_qian)->order('code_key desc')->find()['code_key'];
			$where_n1_c['code_key']=array('gt',$code_key_n);
			$n = $codeModel->where($where_n1_c)->count();
			$data_qian['n'.$i]=$n;
		}
		$ylModel = D("YlQian");
		$data_qian['code_key']=$code_key;
		$data_qian['time']=date("Y-m-d H:i:s",time());
		$result_save_qian = $ylModel->save($data_qian);
		if($result_save_qian==0){
			$result_add_qian = $ylModel->add($data_qian);
		}
		//统计wan位
		for($i=0;$i<=9;$i++){
			$where_wan['h1']=$i;
			$code_key_n= $codeModel->where($where_wan)->order('code_key desc')->find()['code_key'];
			$where_n1_c['code_key']=array('gt',$code_key_n);
			$n = $codeModel->where($where_n1_c)->count();
			$data_wan['n'.$i]=$n;
		}
		$ylModel = D("YlWan");
		$data_wan['code_key']=$code_key;
		$data_wan['time']=date("Y-m-d H:i:s",time());
		$result_save_wan = $ylModel->save($data_wan);
// 		dump($data_wan);
		if($result_save_wan==0){
			$result_add_wan = $ylModel->add($data_wan);
		}
		$this->tjsm14();//推荐杀码
		$this->tjsm23();//推荐杀码
	}
	
	//杀码
	function tjsm14(){
		dump("执行了tjsm14");
		$codeModel=D("CodeTal");
		$code= $codeModel->order('code_key desc')->find();//获取新开奖数据
		
		//获取个位推荐
		$ylModel = D("YlGe");
		$sha_ge_arr = gs_sm14($code['h5']);//获取杀码4个
		$where['code_key']=$code['code_key'];
		$yl_find_ge = $ylModel->where($where)->field('n1,n2,n3,n4,n5,n6,n7,n8,n9,n0')->find();
		foreach ($sha_ge_arr as $key => $value) {
			unset($yl_find_ge['n'.$value]);//去除杀码
		}
		$max_yl_key = array_search(max($yl_find_ge),$yl_find_ge);
		unset($yl_find_ge[$max_yl_key]);//去除剩余最大遗漏
		//获取个位推荐数
		$l=0;
		$data_ge = '';
		foreach ($yl_find_ge as $k_data => $v_data) {
			if($l<=4){
				$data_ge = $data_ge.substr($k_data, 1,1);
			}
			$l++;
		}
		//获取个位推荐
		$ylModel = D("YlShi");
		$sha_shi_arr = gs_sm14($code['h4']);//获取杀码4个
		$where['code_key']=$code['code_key'];
		$yl_find_shi = $ylModel->where($where)->field('n1,n2,n3,n4,n5,n6,n7,n8,n9,n0')->find();
		foreach ($sha_shi_arr as $key => $value) {
			unset($yl_find_shi['n'.$value]);//去除杀码
		}
		$max_yl_key = array_search(max($yl_find_shi),$yl_find_shi);
		unset($yl_find_shi[$max_yl_key]);//去除剩余最大遗漏
		//获取个位推荐数
		$l=0;
		$data_shi = '';
		foreach ($yl_find_shi as $k_data => $v_data) {
			if($l<=4){
				$data_shi = $data_shi.substr($k_data, 1,1);
			}
			$l++;
		}
		//获取bai位推荐
		$ylModel = D("YlBai");
		$sha_bai_arr = gs_sm14($code['h3']);//获取杀码4个
		$where['code_key']=$code['code_key'];
		$yl_find_bai = $ylModel->where($where)->field('n1,n2,n3,n4,n5,n6,n7,n8,n9,n0')->find();
		foreach ($sha_bai_arr as $key => $value) {
			unset($yl_find_bai['n'.$value]);//去除杀码
		}
		$max_yl_key = array_search(max($yl_find_bai),$yl_find_bai);
		unset($yl_find_bai[$max_yl_key]);//去除剩余最大遗漏
		//获取个位推荐数
		$l=0;
		$data_bai = '';
		foreach ($yl_find_bai as $k_data => $v_data) {
			if($l<=4){
				$data_bai = $data_bai.substr($k_data, 1,1);
			}
			$l++;
		}
		//获取qian位推荐
		$ylModel = D("YlQian");
		$sha_qian_arr = gs_sm14($code['h2']);//获取杀码4个
		$where['code_key']=$code['code_key'];
		$yl_find_qian = $ylModel->where($where)->field('n1,n2,n3,n4,n5,n6,n7,n8,n9,n0')->find();
		foreach ($sha_qian_arr as $key => $value) {
			unset($yl_find_qian['n'.$value]);//去除杀码
		}
		$max_yl_key = array_search(max($yl_find_qian),$yl_find_qian);
		unset($yl_find_qian[$max_yl_key]);//去除剩余最大遗漏
		//获取个位推荐数
		$l=0;
		$data_qian = '';
		foreach ($yl_find_qian as $k_data => $v_data) {
			if($l<=4){
				$data_qian = $data_qian.substr($k_data, 1,1);
			}
			$l++;
		}
		
		//获取wan位推荐
		$ylModel = D("YlWan");
		$sha_wan_arr = gs_sm14($code['h1']);//获取杀码4个
		$where['code_key']=$code['code_key'];
		$yl_find_wan = $ylModel->where($where)->field('n1,n2,n3,n4,n5,n6,n7,n8,n9,n0')->find();
		foreach ($sha_wan_arr as $key => $value) {
			unset($yl_find_wan['n'.$value]);//去除杀码
		}
		$max_yl_key = array_search(max($yl_find_wan),$yl_find_wan);
		unset($yl_find_wan[$max_yl_key]);//去除剩余最大遗漏
		//获取个位推荐数
		$l=0;
		$data_wan = '';
		foreach ($yl_find_wan as $k_data => $v_data) {
			if($l<=4){
				$data_wan = $data_wan.substr($k_data, 1,1);
			}
			$l++;
		}
		$data['ge']=$data_ge;
		$data['shi']=$data_shi;
		$data['bai']=$data_bai;
		$data['qian']=$data_qian;
		$data['wan']=$data_wan;
		$data['code_key']=getNextId($code['code_key']);
// 		$data['code_key']=getNewCokeKey();
		$tjsmModel = D("TjSm");
		$result_save = $tjsmModel->save($data);
		
		if($result_save==0){
			$result_add = $tjsmModel->add($data);
			
		}
		dump($result_save);
		dump($result_add);
	}
	//杀码
	function tjsm23(){
		dump("执行了tjsm12");
		$codeModel=D("CodeTal");
		$code= $codeModel->order('code_key desc')->find();//获取新开奖数据
	
		//获取个位推荐
		$ylModel = D("YlGe");
		$sha_ge_arr = gs_sm23($code['h5']);//获取杀码4个
		$where['code_key']=$code['code_key'];
		$yl_find_ge = $ylModel->where($where)->field('n1,n2,n3,n4,n5,n6,n7,n8,n9,n0')->find();
		foreach ($sha_ge_arr as $key => $value) {
			unset($yl_find_ge['n'.$value]);//去除杀码
		}
		$max_yl_key = array_search(max($yl_find_ge),$yl_find_ge);
		unset($yl_find_ge[$max_yl_key]);//去除剩余最大遗漏
		//获取个位推荐数
		$l=0;
		$data_ge = '';
		foreach ($yl_find_ge as $k_data => $v_data) {
			if($l<=4){
				$data_ge = $data_ge.substr($k_data, 1,1);
			}
			$l++;
		}
		//获取个位推荐
		$ylModel = D("YlShi");
		$sha_shi_arr = gs_sm23($code['h4']);//获取杀码4个
		$where['code_key']=$code['code_key'];
		$yl_find_shi = $ylModel->where($where)->field('n1,n2,n3,n4,n5,n6,n7,n8,n9,n0')->find();
		foreach ($sha_shi_arr as $key => $value) {
			unset($yl_find_shi['n'.$value]);//去除杀码
		}
		$max_yl_key = array_search(max($yl_find_shi),$yl_find_shi);
		unset($yl_find_shi[$max_yl_key]);//去除剩余最大遗漏
		//获取个位推荐数
		$l=0;
		$data_shi = '';
		foreach ($yl_find_shi as $k_data => $v_data) {
			if($l<=4){
				$data_shi = $data_shi.substr($k_data, 1,1);
			}
			$l++;
		}
		//获取bai位推荐
		$ylModel = D("YlBai");
		$sha_bai_arr = gs_sm23($code['h3']);//获取杀码4个
		$where['code_key']=$code['code_key'];
		$yl_find_bai = $ylModel->where($where)->field('n1,n2,n3,n4,n5,n6,n7,n8,n9,n0')->find();
		foreach ($sha_bai_arr as $key => $value) {
			unset($yl_find_bai['n'.$value]);//去除杀码
		}
		$max_yl_key = array_search(max($yl_find_bai),$yl_find_bai);
		unset($yl_find_bai[$max_yl_key]);//去除剩余最大遗漏
		//获取个位推荐数
		$l=0;
		$data_bai = '';
		foreach ($yl_find_bai as $k_data => $v_data) {
			if($l<=4){
				$data_bai = $data_bai.substr($k_data, 1,1);
			}
			$l++;
		}
		//获取qian位推荐
		$ylModel = D("YlQian");
		$sha_qian_arr = gs_sm23($code['h2']);//获取杀码4个
		$where['code_key']=$code['code_key'];
		$yl_find_qian = $ylModel->where($where)->field('n1,n2,n3,n4,n5,n6,n7,n8,n9,n0')->find();
		foreach ($sha_qian_arr as $key => $value) {
			unset($yl_find_qian['n'.$value]);//去除杀码
		}
		$max_yl_key = array_search(max($yl_find_qian),$yl_find_qian);
		unset($yl_find_qian[$max_yl_key]);//去除剩余最大遗漏
		//获取个位推荐数
		$l=0;
		$data_qian = '';
		foreach ($yl_find_qian as $k_data => $v_data) {
			if($l<=4){
				$data_qian = $data_qian.substr($k_data, 1,1);
			}
			$l++;
		}
	
		//获取wan位推荐
		$ylModel = D("YlWan");
		$sha_wan_arr = gs_sm23($code['h1']);//获取杀码4个
		$where['code_key']=$code['code_key'];
		$yl_find_wan = $ylModel->where($where)->field('n1,n2,n3,n4,n5,n6,n7,n8,n9,n0')->find();
		foreach ($sha_wan_arr as $key => $value) {
			unset($yl_find_wan['n'.$value]);//去除杀码
		}
		$max_yl_key = array_search(max($yl_find_wan),$yl_find_wan);
		unset($yl_find_wan[$max_yl_key]);//去除剩余最大遗漏
		//获取个位推荐数
		$l=0;
		$data_wan = '';
		foreach ($yl_find_wan as $k_data => $v_data) {
			if($l<=4){
				$data_wan = $data_wan.substr($k_data, 1,1);
			}
			$l++;
		}
		$data['ge']=$data_ge;
		$data['shi']=$data_shi;
		$data['bai']=$data_bai;
		$data['qian']=$data_qian;
		$data['wan']=$data_wan;
		$data['code_key']=getNextId($code['code_key']);
// 		$data['code_key']=getNewCokeKey();
		$tjsmModel = D("TjSm23");
		$result_save = $tjsmModel->save($data);
	
		if($result_save==0){
			$result_add = $tjsmModel->add($data);
		}
		dump($result_save);
		dump($result_add);
	}
	
	//随机排列推荐
	function sjtj(){
		$codeModel=D("CodeTal");
		$code_key= $codeModel->order('code_key desc')->find()['code_key'];//获取新开奖数据
		$sjarr = array('ge','shi','bai','qian','wan');
		foreach ($sjarr as $k_sjarr => $v_sjarr) {
				$arr[$v_sjarr] = gs_sjpl();
				foreach ($arr[$v_sjarr] as $key => $value) {
					$data[$v_sjarr] = $data[$v_sjarr].$value;
				}
		}
		$data['code_key']=getNextId($code_key);
// 		$data['code_key']=getNewCokeKey();
		$tjsmModel = D("TjSj");
		$result_add = $tjsmModel->add($data);
	}
	
	
	//随机排列推荐
	function sjtjall(){
		$codeModel=D("CodeTal");
		$code_key= $codeModel->order('code_key desc')->find()['code_key'];//获取新开奖数据
		$all_arr=array(1,2,3,4,5,6,7,8,9,10);
		foreach ($all_arr as $key_all => $value_all) {
			$data=array();
			$sjarr = array('ge','shi','bai','qian','wan');
			foreach ($sjarr as $k_sjarr => $v_sjarr) {
				$arr[$v_sjarr] = gs_sjpl();
				foreach ($arr[$v_sjarr] as $key => $value) {
					$data[$v_sjarr] = $data[$v_sjarr].$value;
				}
			}
			$data['code_key']=getNextId($code_key);
// 			$data['code_key']=getNewCokeKey();
			$tjsmModel = D("TjSj".$value_all);
// 			dump($data);
			$result_add = $tjsmModel->add($data);
		}
	}
	
	
	//
// 	//借算
// 	function jsall(){
// 		$this->jslr();
// 	}
// 	//结算冷热推荐
// 	function jslr(){
// 		$total_arr=array(10,20,50,100,500,1000);
// 		foreach ($total_arr as $k_total_arr => $v_total_arr) {
// 			$lrtjModel = D("LrTj".$v_total_arr);
// 			$lrlist = $lrtjModel->select();
// 			$codeModel = D("CodeTal");
// 			foreach ($lrlist as $key => $value) {
// 				$where_code['code_key']=$value['code_key'];
// 				$code_res = $codeModel->where($where_code)->find();
// 				dump($code_res);
// 				if($code_res==null){
// 				}else{
// 					//热数据
// 					if(($code_res['h5']==$value['LrGe_r'])||($code_res['h4']==$value['LrShi_r'])||($code_res['h3']==$value['LrBai_r'])||
// 					($code_res['h2']==$value['LrQian_r'])||($code_res['h1']==$value['LrWan_r'])){
// 						$data_wrong['wrong_r']='对';
// 					}else{
// 						$data_wrong['wrong_r']='错';
// 					}
// 					//冷数据
// 					if(($code_res['h5']==$value['LrGe_l'])||($code_res['h4']==$value['LrShi_l'])||($code_res['h3']==$value['LrBai_l'])||
// 					($code_res['h2']==$value['LrQian_l'])||($code_res['h1']==$value['LrWan_l'])){
// 						$data_wrong['wrong_l']='对';
// 					}else{
// 						$data_wrong['wrong_l']='错';
// 					}
// 					$data_wrong['code_key']=$value['code_key'];
// 					$lrtjModel->save($data_wrong);
// 				}
// 			}
// 		}
// 	}
	
	
	
	function get_new_code() {
		$codeModel = D ('Code');
		$result = $codeModel->order('code_key desc')->find();
		echo json_encode($result,JSON_UNESCAPED_UNICODE);
	}
	
	
	//刷新数据
	function new_code(){
		// 		for ($i=0;$i<=1000;$i++){
		// 			$this->js_update();
		// 			sleep(1);
		// 			dump($i);
		// 		}
		// 		die();
	}
	
	//导入数据
	function exceliput(){
		$codeModel = D("Code");
		
		$handler = fopen('./Public/upload/a.txt','r'); //打开文件
		while(!feof($handler)){
			$m[] = fgets($handler,4096); //fgets逐行读取，4096最大长度，默认为1024
		}
		fclose($handler); //关闭文件
		
		S('code_result',$m);
		echo '<pre>';
		print_r(S('code_result'));
		echo '</pre>';
		// 		die();
		// 		$m = S('code_result');
		// 		dump(count($m));
		// 			$mlength = count($m);
		die();
		//输出文件
		
		$p=2;
		
		$j=10000*$p;
		$h=10000*($p-1);
		for($i=$h;$i<=$j+$j;$i++){
			// 		foreach ($m as $key => $value) {
				
			if($i>=$h&&$i<=$j){
				$value = $m[$i];
				dump($value."/".$i);
				$value = trim($value);
				$value = str_replace(array("/r/n", "/r", "/n"), "", $value);
				$data['date']=substr($value, 0,8);
				$data['number']=substr($value, 9,3);
				$data['hm']=substr($value, 13,5);
				$data['h1']=substr($data['hm'], 0,1);
				$data['h2']=substr($data['hm'], 1,1);
				$data['h3']=substr($data['hm'], 2,1);
				$data['h4']=substr($data['hm'], 3,1);
				$data['h5']=substr($data['hm'], 4,1);
				$data['code_key']=$data['date'].$data['number'];
				//后2,对子或空
				if($data['h4']==$data['h5']){
					$data['hou2']='对子';
				}else{
					$data['hou2']='';
				}
				//后3(组六，组三，豹子)
				if($data['h3']==$data['h4']&&$data['h4']==$data['h5']){
					$data['hou3']='豹子';
				}else if($data['h3']==$data['h4']||$data['h3']==$data['h5']||$data['h4']==$data['h5']){
					$data['hou3']='组三';
				}else{
					$data['hou3']='组六';
				}
				$data['time']=date('Y-m-d H:i:s',time());
				// 				$where['code_key']=$data['code_key'];
				// 				$find_result = $codeModel->where($where)->find();
				// 				if($find_result){
				// 					$data['id']=$find_result['id'];
				// 					$save_result = $codeModel->save($data);
				// 				}else{
				$add_result = $codeModel->add($data);
				// 				}
			}
		}
		
		// 		dump($save_result);
		// 		dump($add_result);
		
	}
	
}

//对象转数组
function object_array($array) {
	if(is_object($array)) {
		$array = (array)$array;
	} if(is_array($array)) {
		foreach($array as $key=>$value) {
			$array[$key] = object_array($value);
		}
	}
	return $array;
}







